package ThreadSafety;

public class MultiThreadDemo {
    public static long res = 0;
    public static void main1(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            for (long i = 0; i <= 100_0000_0000L; i++) {
                res = res + i;
            }
        });
        long beginTime = System.currentTimeMillis();
        t.start();
        t.join();
        long endTime = System.currentTimeMillis();
        System.out.println(res);
        System.out.println("time: "+(endTime-beginTime)+"ms");
    }
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            for (long i = 0; i <= 50_0000_0000L; i++) {
                res = res + i;
            }
        });
        Thread t1 = new Thread(() -> {
            for (long i = 50_0000_0001L; i <= 100_0000_0000L; i++) {
                res = res + i;
            }
        });
        long beginTime = System.currentTimeMillis();
        t.start();
        t1.start();
        t.join();
        t1.join();
        long endTime = System.currentTimeMillis();
        System.out.println(res);
        System.out.println("time: "+(endTime-beginTime)+"ms");
    }
}
